﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Weibo4Sharp.Model;

namespace Weibo4SharpWeb.Controllers
{
    public class CommentController : WeiboControllerBase
    {
        //
        // GET: /Comment/

        public ActionResult Index( int id = 1)
        {           
            var comments = GetAuthenticatedService().ListWeiboCommentsToMe(page: id);

            ViewBag.PageId = id;
            ViewBag.HasPrev = id > 1;
            ViewBag.HasNext = comments.Count() == 20;

            if (id == 1)
            {
                GetAuthenticatedService().ResetCount(1);
            }

            if (Request.IsAjaxRequest())
            {
                return PartialView("CommentList", comments);
            }

            return View(comments);
        }

        //
        // GET: /Comment/Detail/

        public ActionResult Detail(long id)
        {
            var s = GetAuthenticatedService();

            var weibo = s.GetWeibo(id);

            if (weibo == null)
            {
                return NewHomeResult();
            }

            s.ListWeiboCountsBindToWeibos(new List<WeiboStatus> { weibo });

            ViewBag.Weibo = weibo;

            var comments = s.ListComments(id);

            //s.ListWeiboCountsBindToWeibos(

            ViewBag.Comments = comments;

            return View();
        }

        //
        // POST /Comment/Create
        [HttpPost]
        public ActionResult Create(long id, string comment)
        {
            var s = GetAuthenticatedService();

            var rtn = s.Comment(id, comment);

            if (Request.IsAjaxRequest())
            {
                return PartialView("_WeiboComment", rtn);
            }           

            return RedirectToAction("Detail", new { id = id });
        }
    }
}
